package com.hsyg.netty.entity;

import java.io.Serializable;
import java.nio.ByteBuffer;

public class UserInfo implements Serializable {

  private static final long serialVersionUID = -7168336573293495706L;

  private String userName;

  private int userID;

  public UserInfo buildUserID(int userID) {
    this.userID = userID;
    return this;
  }

  public UserInfo buildUserName(String userName) {
    this.userName = userName;
    return this;
  }


  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public int getUserID() {
    return userID;
  }

  public void setUserID(int userID) {
    this.userID = userID;
  }

  /**
   * 通用的的序列化
   *
   * @return
   */
  public byte[] codeC() {
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    byte[] value = this.userName.getBytes();
    buffer.putInt(value.length);
    buffer.put(value);
    buffer.putInt(this.userID);
    buffer.flip();

    value = null;
    byte[] result = new byte[buffer.remaining()];
    buffer.get(result);
    return result;
  }

  /**
   * 性能测试版
   *
   * @param buffer
   * @return
   */
  public byte[] codeB(ByteBuffer buffer) {
    buffer.clear();
    byte[] value = this.getUserName().getBytes();
    buffer.putInt(value.length);
    buffer.putInt(this.getUserID());
    buffer.flip();

    value = null;
    byte[] result = new byte[buffer.remaining()];
    buffer.get(result);
    return result;

  }
}
